package com.ruoyi.framework.aspectj;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.aspectj.lang.annotation.DataSource;
import com.ruoyi.framework.datasource.DynamicDataSourceContextHolder;
import org.noear.solon.core.handle.Interceptor;
import org.noear.solon.core.handle.InterceptorChain;

/**
 * @author noear 2021/5/6 created
 */
public class DataSourceAspectNew implements Interceptor {
    @Override
    public Object doIntercept(Object target, Object[] args, InterceptorChain chain) throws Throwable {
        DataSource dataSource = chain.method().getAnnotation(DataSource.class);

        if (StringUtils.isNotNull(dataSource)) {
            DynamicDataSourceContextHolder.setDataSourceType(dataSource.value().name());
        }

        try {
            return chain.doIntercept(target, args);
        } finally {
            // 销毁数据源 在执行方法之后
            DynamicDataSourceContextHolder.clearDataSourceType();
        }
    }
}
